/**
 * 作者: wgx
 * 日期: 2025-02-28 17:41:40
 * 备注: 用户已婚子嗣表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.ChildManager;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;

public class UserChildMarry extends UserChildMarryModel {
    public UserChildMarry() {
        super();
    }

    public UserChildMarry(UserChildUnmarried userChildUnmarried, UserInfo userInfoTarget, String childNameTarget, int quality, int career, int partnerId, int secretaryId, int appearance, long earnAdd) {
        super();
        putUserId(userChildUnmarried.getUserId());
        putName(userChildUnmarried.getName());
        putQuality(userChildUnmarried.getQuality());
        putCareer(userChildUnmarried.getCareer());
        putPartner(userChildUnmarried.getPartner());
        putSecretary(userChildUnmarried.getSecretary());
        putAppearance(userChildUnmarried.getAppearance());
        putSex(userChildUnmarried.getSex());
        putMarryTime(TimeUtils.getCurTime());
        putEarnAdd(userChildUnmarried.getEarnAdd());
        putNameTarget(childNameTarget);
        putQualityTarget(quality);
        putCareerTarget(career);
        putPartnerTarget(partnerId);
        putSecretaryTarget(secretaryId);
        putAppearanceTarget(appearanceTarget);
        putEarnAddTarget(earnAdd);
        if (userInfoTarget != null) {// 跟玩家联姻
            putUserIdTarget(userInfoTarget.getId());
            putUserNameTarget(userInfoTarget.getUserName());
        }
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public JsonObject insert() {
        // 如果超过上限则会删除最早的那条
        long count = Future.await(RedisTools.getHashLen(getListKey()));
        if (count >= AllParam.CHILD_MARRIED_MAX) {
            String orderListKey = EntityManager.getOrderListKey(getListKey());
            String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
            long id = Long.parseLong(delRecord);// dbid
            UserChildMarry userChildMarry = ChildManager.getUserChildMarry(getUserId(), id);
            if (userChildMarry != null) {
                userChildMarry.delete();
            }
        }
        return super.insert();
    }
}
